package org.jeecg.modules.ncmeapis.data.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ncme.digital.hszy.common.utils.JSONHelper;
import ncme.digital.hszy.common.utils.excel.ExcelAreaDBWriter;
import ncme.digital.hszy.common.utils.excel.inner.ExcelFieldOption;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.swagger.BusinessApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@BusinessApi
@Api(tags = "Excel上传")
@RestController
@RequestMapping("/auto/data/excel")
@Slf4j
public class ExcelController {

    @Autowired
    private ExcelAreaDBWriter excelDBWriter;

    @RequiresPermissions("excel:import:db")
    @ApiOperation(value = "导入Excel存表", notes = "")
    @PostMapping(value = "/import/db", consumes = "multipart/form-data")
    public Result<String> cityLookup(@RequestPart("multipartFile") MultipartFile file,
                                     @RequestPart(value = "sheet", required = false) String sheet,
                                     @RequestPart(value = "leftTop", required = false) String leftTop,
                                     @RequestPart(value = "rightBottom", required = false) String rightBottom,
                                     @RequestPart(value = "headHeight", required = false) Integer headHeight,
                                     @RequestPart(value = "tableName", required = false) String tableName
    ) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String defaultTableName = originalFilename
                .substring(0, originalFilename.lastIndexOf("."))
                .replace("-", "_");
        String finalTableName = tableName != null ? tableName : defaultTableName;
        try {
            excelDBWriter.readFromArea(
                    file.getInputStream(),
                    sheet != null ? sheet : "Sheet1",
                    leftTop != null ? leftTop : "A1",
                    rightBottom != null ? rightBottom : "ZZ9999",
                    finalTableName,
                    new ExcelFieldOption() {
                        @Override
                        public Integer getHeadHeight() {
                            return headHeight != null ? headHeight : 1;
                        }
                    }
            );
            excelDBWriter.addTableComment(finalTableName, new JSONHelper().put("fileName", originalFilename).get());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.OK("导入成功！若无数据请检查sheet名称是否正确.");
    }

}
